var cache = require("../../common/Cache.js").cache;
var ArticleTypeCache = require("../../common/ArticleTypeCache.js").AtTypeCache;
var dataFilter = require("../../common/DataFilter.js").dataFilter;
var wxParse = require("../../plugs/wxParse/wxParse.js");
var tools = require("../../common/Tools.js").tools;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    size:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '正在加载',
    })
   
    var typeId = options.typeId;
    var that = this;
    wx.request({
      url: app.macauWebApiList.api_Article(app.macauweburl.webApiUrl,'70'),
      success: function (result) {
        //cache.clear();
        if (result == undefined && result == null) {

          return;
        }


        var filterArticle = dataFilter.getFilter("Type", "EX", result.data.InnerList);

        for (var i = 0; i < filterArticle.length; i++) {
          filterArticle[i].Path = filterArticle[i].Images[0];
          filterArticle[i].ID = filterArticle[i].Code;
        }
        var data = ArticleTypeCache.getByTypeId(typeId);
        console.log("标题是：", data.Title);
        wx.setNavigationBarTitle({
          title: data.Title
        })
       

        app.console.log(data);
        /*tab组件绑定*/
        var c_tab = that.selectComponent("#c_tab");
        that.c_tab = c_tab;

        if (data.Line.length == 0) {
          data.Line.push({
            ID: data.ID,
            Title: "",
            Type: ""
          });
          if (data.Type == "Url") {
            data.Line[0].Type = "Url";
          }
          else if (data.Type == "List") {
            data.Line = filterArticle;
          }
        }


        if (data.Line != undefined && data.Line.length > 0) {
          var articleData = data;
          app.console.log(data);

          // c_tab.setData({
          //   bindData: articleData.Line
          // });

          var swiperPlaneData = [];
          for (var index = 0; index < 1; index++) {
            swiperPlaneData.push({
              "planeIndex": index,
              "planeType": data.Line[index].Type,
              "planeDataId": data.Line[index].Code,
              "hasRender": false
            });
            if (data.Line[index].Type == "Url") {
              swiperPlaneData[index]["planeId"] = "article_" + index;
            } else if (data.Line[index].Type == "EX") {
              swiperPlaneData[index]["planeId"] = "articleList_" + index;
              swiperPlaneData[index]["list"] = data.Line;
            }
            
          }

          that.setData({
            swiperPlaneData: swiperPlaneData
          });

          that._setContent(0, data.Line[0].Code);

          /*绑定tab事件 */
          c_tab.ontabTap(function (event) {
            var index = event.currentTarget.dataset.index;
            var id = data.Line[event.currentTarget.dataset.index].Code;
            wx.navigateTo({
              url: '/pages/ArticleDetails/ArticleDetails?id=' + id,
            })
            that._setContent(index, id, swiperPlaneData);
          });
        } else {
          //var articleList = cache.getSync("allArticle");
          //var targetArticle = dataFilter.getFilter("Type", id.toString(), articleList);



        }

        setTimeout(function () {
          wx.hideLoading()
        }, 500)
      },
      timeout: 10000,
      error: function () {

      }
    })








    //typeId = 350;



  },

  /*设置tab对应内容方法 */
  _setContent: function (index, id) {
    var that=this;
    var swiperPlaneData = that.data.swiperPlaneData;
    var articleList = cache.getSync("allArticle");

    var targetArticle = dataFilter.getFilter("Type", id.toString(), articleList);
    var targetPlane = swiperPlaneData[index];
    app.console.log(that.data);
    if (swiperPlaneData[index]["hasRender"] === false) {
      if (targetPlane.planeType == "EX") {
        console.log(targetPlane.planeId)
        var c_articleList = that.selectComponent("#" + targetPlane.planeId);
        c_articleList.setData({
          list: swiperPlaneData[index].list
        });

        

        /*绑定列表点击事件*/
        c_articleList.onArticleTap(function (event) {
          var id = event.currentTarget.dataset.id;
          wx.navigateTo({
            url: '/pages/ArticleDetails/ArticleDetails?id=' + id,
          })
        });

      } else if (targetPlane.planeType == "Url") {
        wxParse.wxParse('article', 'html', tools.clearInvalidStyle(targetArticle[0].ArContent), this, 5, index);
      }

      swiperPlaneData[index]["hasRender"] = true;

      that.setData({
        "swiperPlaneData": swiperPlaneData
      });

    }

    //wxParse.wxParse('article', 'html', articleDetail.ArContent, that, 5);
    that.setData({
      "swiperIndex": index,
    });
  },

  /*绑定swiper切换方法*/
  swiperChange: function (event) {
    if (event.type != undefined) {
      var currentIndex = event.detail.current;
      this._setContent(currentIndex, this.data.swiperPlaneData[currentIndex].planeDataId)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
     
   
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    wx.showLoading({
      title: '正在加載',
    })
    var that=this;
    var size = this.data.size + 1;
    that.setData({
      size:size
    })
    console.log(size);
    
    
    console.log(this.data.size);

    
    wx.request({
      url: app.macauWebApiList.api_Article(app.macauweburl.webApiUrl, (70 + (size * 50))),
      // url: app.macauWebApiList.api_Article(app.macauweburl.webApiUrl, '3500'),
      success: function (result) {
        var filterArticle = dataFilter.getFilter("Type", "EX", result.data.InnerList);
        console.log(filterArticle)
        if (result.data.PageSize > result.data.Count){
          wx.showLoading({
            title: '全部已加載！',
          })
          setTimeout(function () {
            wx.hideLoading()
          }, 1000)
        }else{

        //cache.clear();
        if (result == undefined && result == null) {

          return;
        }

        
        for (var i = 0; i < filterArticle.length; i++) {
          filterArticle[i].Path = filterArticle[i].Images[0];
          filterArticle[i].ID = filterArticle[i].Code;
        }
        var data = {
          Line: [],
          Type: 'List'
        };

        if (data.Line.length == 0) {
          data.Line.push({
            ID: data.ID,
            Title: "",
            Type: ""
          });
          if (data.Type == "Url") {
            data.Line[0].Type = "Url";
          }
          else if (data.Type == "List") {
            data.Line = filterArticle;
          }
        }


        if (data.Line != undefined && data.Line.length > 0) {
          var articleData = data;
          app.console.log(data);

          // c_tab.setData({
          //   bindData: articleData.Line
          // });

          var swiperPlaneData = [];
          for (var index = 0; index < 1; index++) {
            swiperPlaneData.push({
              "planeIndex": index,
              "planeType": data.Line[index].Type,
              "planeDataId": data.Line[index].Code,
              "hasRender": false
            });
            if (data.Line[index].Type == "Url") {
              swiperPlaneData[index]["planeId"] = "article_" + index;
            } else if (data.Line[index].Type == "EX") {
              swiperPlaneData[index]["planeId"] = "articleList_" + index;
              swiperPlaneData[index]["list"] = data.Line;
            }

          }

          that.setData({
            swiperPlaneData: swiperPlaneData
          });

          that._setContent(0, data.Line[0].Code);

          
        } else {
          //var articleList = cache.getSync("allArticle");
          //var targetArticle = dataFilter.getFilter("Type", id.toString(), articleList);



        }
        }
        setTimeout(function () {
          wx.hideLoading()
        }, 1000)
      },
      timeout: 10000,
      error: function () {

      }
    })   

   
    
 
    //上拉加载
    wx.stopPullDownRefresh();
   
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})